//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Research.ScientificWorkflow.TridentUtilities;
using System.ComponentModel;
using System;
using System.Windows.Data;

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Import
{
    /// <summary>
    /// Interaction logic for AssemblySelectDialog.xaml
    /// </summary>
    public partial class AssemblySelectDialog : Window
    {
        private Collection<AssemblyMetadata> assemblies;

        /// <summary>
        /// Initialize a new instance of the Assembly select window.
        /// </summary>
        /// <param name="assemblies">The list of assembly to be displayed to the user.</param>
        public AssemblySelectDialog(Collection<AssemblyMetadata> assemblies)
        {
            this.assemblies = assemblies;
            InitializeComponent();
            this.assemblyListView.ItemsSource = this.assemblies;
            ICollectionView view = CollectionViewSource.GetDefaultView(this.assemblies);
            TextSearchFilter.SearchFilter(view, this.txtBxSearch);
            this.assemblyListView.SelectedIndex = -1;
            if (assemblies.Count == 0)
            {
                this.okButton.IsEnabled = false;
            }
        }

        /// <summary>
        /// The selected assembly metadata.
        /// </summary>
        public AssemblyMetadata SelectedAssembly { get; private set; }

        /// <summary>
        /// The event handler for the Ok button click.
        /// </summary>
        /// <param name="sender">The origin of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void OKButtonClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.SelectedAssembly = this.assemblyListView.SelectedItem as AssemblyMetadata;
            this.assemblies.Clear();
            this.Close();
        }

        /// <summary>
        /// The event handler for the cancel/close button click.
        /// </summary>
        /// <param name="sender">The origin of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.assemblies.Clear();
            this.Close();
        }

        /// <summary>
        /// The event handler for the left mouse button down on the main border. Used to drag window.
        /// </summary>
        /// <param name="sender">The origin of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void BorderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// The event handler for the assembly list selection changed event.
        /// </summary>
        /// <param name="sender">The origin of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void AssemblyListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Enable the Ok button if the user has selected an assembly.
            if (this.assemblyListView.SelectedItem != null)
            {
                this.SelectedAssembly = this.assemblyListView.SelectedItem as AssemblyMetadata;
                this.okButton.IsEnabled = true;
            }
            else
            {
                this.okButton.IsEnabled = false;
            }
        }
    }

    /// <summary>
    /// Helper class for text filtering.
    /// </summary>
    public static class TextSearchFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TextSearchFilter"/> class.
        /// </summary>
        /// <param name="filteredView">The filtered view.</param>
        /// <param name="textBox">The text box.</param>
        public static void SearchFilter(ICollectionView filteredView, TextBoxWatermark textBox)
        {
            string filterText = string.Empty;
            filteredView.Filter = delegate(object obj)
            {
                if (textBox.IsWatermarked)
                    return true;

                if (string.IsNullOrEmpty(filterText))
                    return true;

                string assemblyName = string.Empty;
                AssemblyMetadata asmMeta = obj as AssemblyMetadata;
                if (asmMeta != null)
                {
                    assemblyName = asmMeta.Name;
                }

                if (string.IsNullOrEmpty(assemblyName))
                    return false;

                int index = assemblyName.IndexOf(filterText, 0, StringComparison.OrdinalIgnoreCase);
                return index > -1;
            };

            textBox.TextChanged += delegate
            {
                filterText = textBox.Text;
                filteredView.Refresh();
            };
        }
    }
}
